/**
 * 
 */
package cn.suse.android.adapter;

import java.util.Date;
import java.util.List;

import cn.suse.android.R;
import cn.suse.android.entity.FriendsRecord;
import cn.suse.android.entity.Video;
import cn.suse.android.share.MainApplication;
import cn.suse.android.utils.StringUtils;
import cn.suse.android.utils.TimeUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author liubin
 * Create DateTime:Oct 18, 2012 10:08:19 AM
 * Description:
 * @Version: 1.0.0
 */
public class FriendRecordAdapter extends BaseAdapter {

	private Context context;
	private List<FriendsRecord> lists;
	
	public FriendRecordAdapter(Context context, List<FriendsRecord> lists) {
		super();
		this.context = context;
		this.lists = lists;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists == null?0:lists.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lists == null?null:lists.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		View view = convertView;
		if(view == null){
			holder = new ViewHolder();
			view = LayoutInflater.from(context).inflate(R.layout.friend_record_listitem, null);
			holder.headerImageView = (ImageView) view.findViewById(R.id.header);
			holder.usernameTextView = (TextView)view.findViewById(R.id.username);
			holder.commentTextView = (TextView)view.findViewById(R.id.commentText);
			holder.commentImageView = (ImageView) view.findViewById(R.id.comment);
			holder.contentTextView = (TextView)view.findViewById(R.id.content);
			holder.redirectImageView = (ImageView) view.findViewById(R.id.redirect);
			holder.redirectTextView = (TextView) view.findViewById(R.id.redirectText);
			holder.sourceImageView = (ImageView) view.findViewById(R.id.image);
			holder.timeTextView = (TextView) view.findViewById(R.id.time);
			view.setTag(holder);
		}else{
			holder = (ViewHolder)view.getTag();
		}
		FriendsRecord friendsRecord = lists.get(position);
		if(StringUtils.convertToInt(friendsRecord.getCnum())>=0){
			holder.commentTextView.setText(friendsRecord.getCnum());
		}else{
			holder.commentTextView.setText("0");
		}
		if(StringUtils.convertToInt(friendsRecord.getRnum())>=0){
			holder.redirectTextView.setText(friendsRecord.getRnum());
		}else{
			holder.redirectTextView.setText("0");
		}
		if(!StringUtils.isEmpty(friendsRecord.getUser().getLogo50())&&StringUtils.isImage(friendsRecord.getUser().getLogo50())){
			MainApplication.bitmapManager.loadBitmap(friendsRecord.getUser().getLogo50(), holder.headerImageView);
		}else{
			holder.headerImageView.setImageResource(R.drawable.widget_dface_loading);
		}
		holder.usernameTextView.setText(friendsRecord.getUser().getName());
		holder.timeTextView.setText(TimeUtils.friendly_time(TimeUtils.dateFormater.format(new Date(Long.parseLong(friendsRecord.getCtime()+"000")))));
		holder.contentTextView.setText(friendsRecord.getContent());
		if(!StringUtils.isEmpty(friendsRecord.getVideos())){
			for(Video video:friendsRecord.getVideos()){
				if(!StringUtils.isEmpty(video.getImg())&&StringUtils.isImage(video.getImg())){
					holder.sourceImageView.setVisibility(View.VISIBLE);
					MainApplication.bitmapManager.loadBitmap(friendsRecord.getVideos().get(0).getImg(), holder.sourceImageView);
					break;
				}
			}
		}else if(!StringUtils.isEmpty(friendsRecord.getPics())){
			for(String pic:friendsRecord.getPics()){
				if(!StringUtils.isEmpty(pic)&&StringUtils.isImage(pic)){
					holder.sourceImageView.setVisibility(View.VISIBLE);
					MainApplication.bitmapManager.loadBitmap(pic, holder.sourceImageView);
					break;
				}
			}
		}
		return view;
	}
	private static class ViewHolder{
		ImageView headerImageView;
		TextView usernameTextView;
		TextView timeTextView;
		TextView contentTextView;
		TextView redirectTextView;
		TextView commentTextView;
		ImageView sourceImageView;
		ImageView redirectImageView;
		ImageView commentImageView;
	}
	public List<FriendsRecord> getLists() {
		return lists;
	}

	public void setLists(List<FriendsRecord> lists) {
		this.lists = lists;
		this.notifyDataSetChanged();
	}
	
}
